<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;
use app\admin\model\Admin as AdminModel;
use app\admin\model\AdminMenu;

class Index extends Admin {

    /**
     * 控制台首页
     * @return type
     */
    public function index() {
        return $this->fetch();
    }

    /**
     * 管理员面板
     */
    public function profile() {
        return $this->fetch();
    }

    /**
     * 登录页
     */
    public function login(Request $request) {
        $this->view->engine->layout(false);
        $admin = session('admin');
        if ($admin) {
            $this->redirect('index/index');
        }

        $referer = $request->param('url');

        if ($request->isPost()) {
            $email = $request->param('email');
            $passwod = $request->param('password');

            $adminModel = new AdminModel();
            $result = $adminModel->login($email, $passwod);

            if ($result['code'] === 0) {
                session('admin', $result['data']); //用户信息

                $menuModel = new AdminMenu();
                $menu = $menuModel->getLeftMenu();
                session('leftmenu', $menu); //菜单信息

                $this->redirect($referer ? base64_decode($referer) : 'index/index');
                exit();
            } else {
                $this->assign('errorMsg', $result['msg']);
            }
        }
        $this->assign('referer', $referer);

        return $this->fetch();
    }

    /**
     * 退出
     */
    public function logout() {
        session(null);
        $this->redirect('index/login');
    }

}
